package com.ycy.temperature.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jude.utils.JUtils;
import com.xyzlf.poplib.PopCommon;
import com.xyzlf.poplib.PopModel;
import com.ycy.temperature.R;
import com.ycy.temperature.bean.ActivityBean;
import com.ycy.temperature.bean.VerificationBean;
import com.ycy.temperature.date.net.UserNetWorks;
import com.ycy.temperature.view.view.MyDialog;
import com.ycy.temperature.view.view.RoundImageView;
import com.zhy.autolayout.AutoLinearLayout;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import rx.Subscriber;

/**
 * @author Jirh
 */
public class ActivityMeAdapter extends BaseQuickAdapter<ActivityBean.DataBean, BaseViewHolder> {
    private MyDialog myDialog;

    public ActivityMeAdapter(int layoutResId, List data) {
        super(layoutResId, data);
    }

    public void deleteItem(int position) {
        mData.remove(position);
        notifyDataSetChanged();
    }

    @Override
    protected void convert(final BaseViewHolder holder, final ActivityBean.DataBean cate) {

        holder.setIsRecyclable(false);
        RoundImageView ImageHeadImage = holder.getView(R.id.Image_HeadImage);
        final AutoLinearLayout LayoutActivity = holder.getView(R.id.Layout_Activity);
        TextView TextNickName = holder.getView(R.id.Text_NickName);
        TextView TextReleaseaddress = holder.getView(R.id.Text_Releaseaddress);
        TextView TextContent = holder.getView(R.id.Text_Content);
        TextView TextTitle = holder.getView(R.id.Text_Title);
        TextView TextTime = holder.getView(R.id.Text_Time);
        final ImageView ButtonGO = holder.getView(R.id.Image_Direction);
        AutoLinearLayout LayoutLabel = holder.getView(R.id.Layout_Label);
        Glide.with(mContext)
                .load("http://" + cate.getActivityUserMessage().getHeadimage())
                .apply(new RequestOptions()
                        .centerCrop()
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(ImageHeadImage);
        TextNickName.setText(cate.getActivityUserMessage().getNickname());
        TextReleaseaddress.setText(cate.getReleaseaddress());
        TextContent.setText(cate.getContent());
        TextTitle.setText(cate.getTitle());
        TextTime.setText("时间:" + cate.getActivitytime());
        String[] arr = cate.getLableid().replace("[", "").replace("]", "").split(",");
        List<String> CirleList = Arrays.asList(arr);
        for (int i = 0; i < CirleList.size(); i++) {
            addOrderItem(CirleList.get(i), LayoutLabel);
        }
        ButtonGO.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showItemPop(ButtonGO, cate.getActivityUserMessage().getActivityid(), holder.getAdapterPosition() + "");

            }
        });
    }

    public void addOrderItem(final String LabelList, LinearLayout ll_lv) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_activity_label, null);
        TextView TextLabel = view.findViewById(R.id.Text_Label);
        TextLabel.setText(LabelList);
        ll_lv.addView(view);
    }

    private String getDate(String date) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long diff;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            diff = System.currentTimeMillis() - sdf.parse(date).getTime();
            day = diff / (24 * 60 * 60);
            hour = (diff / (60 * 60) - day * 24);
            min = ((diff / 60) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (day != 0) {
            if (day >= 2) {
                return date;
            } else {
                return day + "天前";
            }
        }
        if (hour != 0) return hour + "小时前";
        if (min != 0) return min + "分钟前";
        if (sec != 0) return "1分钟前";
        return "0秒";

    }

    private void showItemPop(View targetView, final String Activityid, final String pid) {
        int location[] = new int[2];
        targetView.getLocationOnScreen(location);
        int x = dp2px(mContext, 15);
        int y = location[1] + targetView.getHeight();

        PopModel feedPopModel = new PopModel();
        feedPopModel.setItemDesc("删除");

//        PopModel messagePopMode = new PopModel();
//        messagePopMode.setItemDesc("添加收藏");

        final List<PopModel> list = new ArrayList<>();
        list.add(feedPopModel);
//        list.add(messagePopMode);


        PopCommon popCommon = new PopCommon((Activity) mContext, list, new PopCommon.OnPopCommonListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, final long id) {
                myDialog = new MyDialog(mContext, R.style.MyDialog);
                myDialog.setTitle("");
                myDialog.setMessage("\n您有正在进行中的任务，\n是否继续删除？\n");
                myDialog.setYesOnclickListener("确定", new MyDialog.onYesOnclickListener() {
                    @Override
                    public void onYesOnclick() {
                        getDeleteActivity("0", Activityid, pid);
                        myDialog.dismiss();
                    }
                });
                myDialog.setNoOnclickListener("取消", new MyDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        myDialog.dismiss();
                    }
                });
                myDialog.show();
            }

            @Override
            public void onDismiss() {

            }
        });
        popCommon.showPop(targetView, x, y);
    }

    private static int dp2px(Context context, float value) {
        return (int) (context.getResources().getDisplayMetrics().density * value + 0.5);
    }

    public void getDeleteActivity(String type, String activityOrCircleId, final String id) {
        UserNetWorks.getDeleteActivity(type, activityOrCircleId, new Subscriber<VerificationBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(VerificationBean verificationBean) {
                if (verificationBean.getResponseState().equals("1")) {
                    JUtils.Toast(verificationBean.getMsg());
                    deleteItem(Integer.parseInt(id));
                } else {
                    JUtils.Toast(verificationBean.getMsg());
                }
            }
        });
    }
}
